import java.io.IOException;

import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;

public class Main {

	public static void main(String[] args) {
		
		// Create database schema
		Schema teacherAppSchema = new Schema(1, "de.androidDevelopers.communityTeacherApp.models");
		
		// Create the class info
		Entity classEntity = addClassInfo(teacherAppSchema);
		
		// Create the room info
        Entity roomEntity = addRoomInfo(teacherAppSchema, classEntity);

        // Create the contact info
     	Entity contactEntity = addContactInfo(teacherAppSchema);
     	
        // Create the pupil info
        Entity pupilEntity = addPupilInfo(teacherAppSchema, classEntity, contactEntity);
     	
     	//Create subject
     	Entity subjectEntity = addSubjectInfo(teacherAppSchema,classEntity,pupilEntity);
        
     	//Create homework
     	Entity homeworkEntity = addHomeworkInfo(teacherAppSchema);
        
        //Create classbook
     	addClassbook(teacherAppSchema,classEntity,roomEntity,homeworkEntity, subjectEntity);
		
     	//Create grade
     	addGrade(teacherAppSchema, classEntity, subjectEntity, pupilEntity);
     	
		try {
			new DaoGenerator().generateAll(teacherAppSchema, "./src-gen");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Entity addRoomInfo(Schema databaseSchema, Entity classEntity)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity roomEntity = databaseSchema.addEntity("RoomInfo");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	roomEntity.implementsSerializable();

        	// Create the _id database field.
        	roomEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Number of the classroom.
        	roomEntity.addIntProperty("Room_Number");
        
        	// The equipment in this room (should be a to-many-reference to an Equipment object later on)
        	// Could be implemented as CSV or JSON at this point of development.
        	roomEntity.addStringProperty("Equipment");
        
        /* Foreign Keys */
        	
        	// ClassFK
        	Property ClassFKProperty = roomEntity.addLongProperty("ClassFK").getProperty();
        	roomEntity.addToOne(classEntity, ClassFKProperty);

        	return roomEntity;
    }

    public static Entity addPupilInfo(Schema databaseSchema, Entity classEntity, Entity contactEntity)
    {
    	/* Create Schema, ID-Property and KeepSections */
    	
        	// Adding desired entity to database schema
    		Entity pupilEntity = databaseSchema.addEntity("PupilInfo");
    		// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
    		pupilEntity.implementsSerializable();

    		// Since I want to have some computed properties we need to keep some sections
    		pupilEntity.setHasKeepSections(true);

    		// Create the _id database field.
    		pupilEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
    		
    		/* Want computed property 'getName()' which returns forename[space]surname */
    		// Forename
    		pupilEntity.addStringProperty("Forname");
    		
    		// Surname
    		pupilEntity.addStringProperty("Surname");

    		/* Want computed property 'age()' which returns the age according to birthday and current day */
    		// Date of birth
    		pupilEntity.addStringProperty("Birthday");

    		// Reference number
    		pupilEntity.addStringProperty("Reference_Number");
        
    		// PictureID
    		pupilEntity.addIntProperty("PictureID");
    		
        /* Foreign Keys */
        	
    		// ClassFK
        	Property ClassFKProperty = pupilEntity.addLongProperty("ClassFK").getProperty();
        	pupilEntity.addToOne(classEntity, ClassFKProperty);
    		
        	// ContactInfoFK
        	Property ContactInfoFKProperty = pupilEntity.addLongProperty("ContactInfoFK").getProperty();
        	pupilEntity.addToMany(contactEntity, ContactInfoFKProperty);
        
        return pupilEntity;
    }
    
    public static Entity addClassInfo(Schema databaseSchema)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity classEntity = databaseSchema.addEntity("ClassInfo");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	classEntity.implementsSerializable();

        	// Create the _id database field.
        	classEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Name of the class
        	classEntity.addStringProperty("Name");
        
        	// Name of the teacher (maybe optional later)
        	classEntity.addStringProperty("Teacher");
        
        	return classEntity;
    }

    public static Entity addContactInfo(Schema databaseSchema)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity contactEntity = databaseSchema.addEntity("ContactInfo");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	contactEntity.implementsSerializable();

        	// Create the _id database field.
        	contactEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Name of the contact
        	contactEntity.addStringProperty("Name");
        
        	// Adress
        	contactEntity.addStringProperty("Adress");

        	// Relation
        	contactEntity.addStringProperty("Relation");
        	
        	// Phone
        	contactEntity.addStringProperty("Phone");
        	
        	return contactEntity;
    }

    public static void addClassbook(Schema databaseSchema, Entity classEntity, Entity roomEntity, Entity homeworkEntity, Entity subjectEntity)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity classbookEntity = databaseSchema.addEntity("Classbook");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	classbookEntity.implementsSerializable();

        	// Create the _id database field.
        	classbookEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Date of Entry
        	classbookEntity.addStringProperty("Date");
        
        	// Topic
        	classbookEntity.addStringProperty("Topic");
        	
        	// Content
        	classbookEntity.addStringProperty("Content");
        	
        	// Lesson of the Day
        	classbookEntity.addStringProperty("Lesson");
        
        	
         /* Foreign Keys */
        	
        	// ClassFK
            Property ClassFKProperty = classbookEntity.addLongProperty("ClassFK").getProperty();
            classbookEntity.addToOne(classEntity, ClassFKProperty);
        		
            // RoomInfoFK
            Property RoomInfoFKProperty = classbookEntity.addLongProperty("RoomInfoFK").getProperty();
            classbookEntity.addToOne(roomEntity, RoomInfoFKProperty);
            
            // HomeworkInfoFK
            Property HomeworkInfoFKProperty = classbookEntity.addLongProperty("HomeworkInfoFK").getProperty();
            classbookEntity.addToOne(homeworkEntity, HomeworkInfoFKProperty);
            
            // SubjectInfoFK
            Property SubjectInfoFKProperty = classbookEntity.addLongProperty("SubjectInfoFK").getProperty();
            classbookEntity.addToOne(subjectEntity, SubjectInfoFKProperty);
            
    }
    
    public static Entity addSubjectInfo(Schema databaseSchema, Entity classEntity, Entity pupilEntity)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity subjectEntity = databaseSchema.addEntity("SubjectInfo");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	subjectEntity.implementsSerializable();

        	// Create the _id database field.
        	subjectEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Name
        	subjectEntity.addStringProperty("Name");
        
        	// Kind of Subject
        	subjectEntity.addStringProperty("Typ");

        /* Foreign Keys */
        	
        	// ClassFK
            Property ClassFKProperty = subjectEntity.addLongProperty("ClassFK").getProperty();
            subjectEntity.addToOne(classEntity, ClassFKProperty);
        		
            // PupilInfoFK
            Property PupilInfoFKProperty = subjectEntity.addLongProperty("PupilInfoFK").getProperty();
            subjectEntity.addToMany(pupilEntity, PupilInfoFKProperty);
			
        return subjectEntity;
            
    }
    
    public static Entity addHomeworkInfo(Schema databaseSchema)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity homeworkEntity = databaseSchema.addEntity("HomeworkInfo");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	homeworkEntity.implementsSerializable();

        	// Create the _id database field.
        	homeworkEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Task
        	homeworkEntity.addStringProperty("Task");
        
        	// Date Till
        	homeworkEntity.addStringProperty("Till");

        	// Date Get
        	homeworkEntity.addStringProperty("Get");
        	
        return homeworkEntity;
        	
    }
    
    public static void addGrade(Schema databaseSchema, Entity classEntity, Entity subjectEntity, Entity pupilEntity)
    {
		/* Create Schema and ID-Property */
		
        	// Adding desired entity to database schema
        	Entity gradeEntity = databaseSchema.addEntity("Grade");
        	// Implement the serializable interface, so we can put it into a bundle, activity extra or kinda
        	gradeEntity.implementsSerializable();

        	// Create the _id database field.
        	gradeEntity.addIdProperty();

        /* Create the specific database fields. */
        /* Normal Properties */
        
        	// Date of Entry
        	gradeEntity.addStringProperty("Date");
        
        	// Grade
        	gradeEntity.addStringProperty("Grade");
        	
        	// Type of Grade
        	gradeEntity.addStringProperty("Typ");
        
        	
         /* Foreign Keys */
        	
        	// ClassFK
            Property ClassFKProperty = gradeEntity.addLongProperty("ClassFK").getProperty();
            gradeEntity.addToOne(classEntity, ClassFKProperty);
        		
            // PupilInfoFK
            Property PupilInfoFKProperty = gradeEntity.addLongProperty("PupilInfoFK").getProperty();
            gradeEntity.addToOne(pupilEntity, PupilInfoFKProperty);
            
            // SubjectInfoFK
            Property SubjectInfoFKProperty = gradeEntity.addLongProperty("SubjectInfoFK").getProperty();
            gradeEntity.addToOne(subjectEntity, SubjectInfoFKProperty);
            
    }
    
}
